/* Packages */
package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.gatech.team404.Account;
import edu.gatech.team404.AccountType;
import edu.gatech.team404.Lab;
import edu.gatech.team404.TeamAssignment;
import edu.gatech.team404.dal.AccountDAL;
import edu.gatech.team404.dal.LabDAL;
import edu.gatech.team404.tool.Log;
import edu.gatech.team404.tool.Scheduler;
import edu.gatech.team404.tool.StatusCode;

/**
 * Servlet implementation class UpdateLab
 * @author Michael Dandy
 * @version 1.0
 */
@WebServlet("/UpdateLab")
public class UpdateLab extends HttpServlet {
	/** Serial Number */
	private static final long serialVersionUID = 1L;
       
	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final HttpSession session = request.getSession(true);
        final PrintWriter out = response.getWriter();
        final String statusTag = "labUpdateStatus";
        
        final String courseName = request.getParameter("courseName");
        final LabDAL dal = new LabDAL();
        
        try {
           final String labId = request.getParameter("labId");
           final String name = request.getParameter("name");
           final String startDate = request.getParameter("startDate");
           final String endDate = request.getParameter("endDate");
           final String assignment = request.getParameter("teamAssignment");
           final String size = request.getParameter("teamSize");

           boolean isValid = true;
           TeamAssignment teamAssignment = TeamAssignment.UNKNOWN;
           int teamSize = 1;
           
           //parse team assignment
           if (assignment.equalsIgnoreCase("Individual")) {
        	   teamAssignment = TeamAssignment.INDIVIDUAL;
           }
           else if (assignment.equalsIgnoreCase("Random")) {
        	   teamAssignment = TeamAssignment.RANDOM;
           }
           else if (assignment.equalsIgnoreCase("Self Select")) {
        	   teamAssignment = TeamAssignment.SELF_SELECT;
           }
           
           //team size
           try {
           	teamSize = Integer.parseInt(size);
           }
           catch (NumberFormatException e) {
           	Log.error("LabServlet", e);
           	isValid = false;
           }
           
           //Update lab
	       final Lab lab = new Lab (courseName, labId, name, "", 
	    		   Scheduler.convertToDate(startDate), Scheduler.convertToDate(endDate), 
	    		   teamAssignment, teamSize);
	           
	       isValid = dal.updateLab(lab);
           
           //Set status
           if(isValid) {
        	   session.setAttribute(statusTag, StatusCode.LAB_UPDATE_SUCCESS);
           }
           else{
        	   session.setAttribute(statusTag, StatusCode.LAB_UPDATE_FAIL);
           }
        }
        finally {
        	session.setAttribute("feed", StatusCode.UPDATE.toString());
        	response.sendRedirect("lab.jsp?course=" 
        			+ URLEncoder.encode(courseName, "UTF-8"));
        	dal.close();
        	out.close();
        }
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		 processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		 processRequest(request, response);
	}

}
